<?php

namespace app\uniapp\controller;

use app\common\exception\MiException;
use app\common\service\CommonAreaService;
use app\model\sys\Area;
use app\Request;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;

class AreaController extends UniController
{
    /**
     * 所有地区数据
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    function getAllTree(): Json
    {
        $tree = CommonAreaService::instance()->getAllChildrenTree();
        return jsonSuccess([
            'tree' => $tree
        ]);
    }

    /**
     * 腾讯地图逆地理编码
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws MiException
     * @throws ModelNotFoundException
     */
    public function geocoder(Request $request): Json
    {
        $location = $request->param('location');
        $arrContextOptions = [
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false,
            ]
        ];
        $data = file_get_contents('https://apis.map.qq.com/ws/geocoder/v1/?location='.$location.'&key=F2GBZ-SREWQ-A3K56-GSLK5-ELOHS-PRB2X',false,stream_context_create($arrContextOptions));
        $data = json_decode($data,true);
        if ($data['status'] == 0){

            $adcode = $data['result']['ad_info']['adcode'];
            // 新的location坐标
            $location = $data['result']['ad_info']['location'];
            // 到数据库查询
            $area = Area::where('code', $adcode)->find();
            if ($area->type == 3){
                $district = $area;
                $city = Area::where('id', $area->pid)->find();
                $province = Area::where('id', $city->pid)->find();
            }elseif($area->type == 2){
                $city = $area;
                $province = Area::where('id', $city->pid)->find();
                $district = Area::where('pid', $city->id)->find();
            }elseif ($area->type == 1){
                $province = $area;
                $city = Area::where('pid', $province->id)->find();
                $district = Area::where('pid', $city->id)->find();
            }

            if ($province && $city && $district){
                return jsonSuccess([
                    'info' => [
                        'p_id' => $province->id,
                        'c_id' => $city->id,
                        'd_id' => $district->id,
                        'p_title' => $province->title,
                        'c_title' => $city->title,
                        'd_title' => $district->title,
                        'location' => $location,
                        'area_title' => "{$province->title} {$city->title} {$district->title}"
                    ]
                ]);
            }else{
                throw new MiException("缺少地区信息");
            }

        }else{
            throw new MiException($data['message']);
        }

    }
}
